using UnityEngine;
using System.Collections;

[System.Serializable]
public class MonsterWalkingPast : MonoBehaviour {
	
	public string attackAnimation;
	public string idleAnimation;
	public string walkAnimation;
	
	public MonsterMove monsterMoveScript;
	public GameObject player;
	public float distanceFromPlayerToCalculateRandomness = 2.0f;
	
	public string playerTag;
	
	public float percentChanceToGrowlAndAttackPlayer = 12.5f;
	public float percentChanceToHitPlayer = 3.0f;
	public int riseAnxietyLevelByIfGrowls = 2;
	public int riseAnxietyLevelByIfHits = 4;
	
	public int riseAnxietyLevelIfMonsterNOTGrowlsAttacks = 1;
	
	private bool triggered = false;
	private bool calculating = false;
	private bool eventDone = false;
	private float startTime = -1f;
	private bool monsterIsCloseToPlayer = false;
	
	public AudioSource monsterMouth;
	public AudioClip monsterGrowl;
	public AudioClip poof;
	
	public AudioSource playerHearth;
	public AudioClip heartbeat;
	public AudioSource playerMouth;
	public AudioClip breathingModerate;
	public AudioClip breathingFast;
	public AudioClip breathingVeryFast;
	
	public CharacterMotor characterMotor;
	public FPSInputController fpsInputController;
	
	public float waitSecondsBeforeVanish = 1.1f;
	
	public bool changeAnimationSpeed = false;
	
	public NavPoint playerNavPoint;
	
	public ParticleEmitter[] particlesWhenPoof;
	public Light particleGlowWhenPoof;
	
	private float oldSpeed = 1.0f;
	
	private bool growls = false;
	
	// Use this for initialization
	void Start () {	
		oldSpeed = monsterMoveScript.speed;
		if (particlesWhenPoof != null) {
			foreach (ParticleEmitter ps in particlesWhenPoof) {
				ps.enabled = false;
			}
		}
		
		if (particleGlowWhenPoof != null) {
			particleGlowWhenPoof.enabled = false;
		}
		
		if (monsterMoveScript.monster.audio != null) {
			monsterMoveScript.monster.audio.enabled = false;
		}
	}
	
	// Update is called once per frame
	void Update () {
		if (triggered) {
			float distance = Vector3.Distance(player.transform.position, monsterMoveScript.monster.transform.position);
			
			Debug.Log("Distance to player: " + distance);
						
			if (distance <= distanceFromPlayerToCalculateRandomness && !monsterIsCloseToPlayer) {
				Debug.Log("Monster is close!");
				monsterIsCloseToPlayer = true;
				monsterMoveScript.triggered = false;
				monsterMoveScript.monster.animation.Play(idleAnimation);
				startTime = Time.time;
			} else if (Game.game.isPlayerInsideSafePlace) {
				monsterMoveScript.nextNavPoint = Game.game.moveStalkingMonsterToThisWhenPLayerIsSafe;
				if (Vector3.Distance(monsterMoveScript.monster.transform.position, 
									 Game.game.moveStalkingMonsterToThisWhenPLayerIsSafe.transform.position) 
					> Game.game.moveStalkingMonsterToThisWhenPLayerIsSafe.threshold) {
					monsterMoveScript.rotateTowards.target = Game.game.moveStalkingMonsterToThisWhenPLayerIsSafe.transform;
				} else {
					monsterMoveScript.triggered = false;
					monsterMoveScript.monster.animation.Play(idleAnimation);
					monsterMoveScript.rotateTowards.target = player.transform;
				}
			}
			if (!Game.game.isPlayerInsideSafePlace && !monsterIsCloseToPlayer) {	
				monsterMoveScript.monster.animation.Play(walkAnimation);
				monsterMoveScript.rotateTowards.target = player.transform;
				monsterMoveScript.nextNavPoint = playerNavPoint;
				monsterMoveScript.triggered = true;
			}
		}		
			
		if (monsterIsCloseToPlayer) {
			if (!calculating && !eventDone) {
				growls = (Game.game.random.Next(1, (int) (100/percentChanceToGrowlAndAttackPlayer)) == 1);
				calculating = true;
				
				if (growls) {						
					monsterMoveScript.monster.animation.Play(attackAnimation);
					monsterMouth.PlayOneShot(monsterGrowl);
					bool hits = (Game.game.random.Next(1, (int)(100/percentChanceToHitPlayer)) == 1);						
					if (hits) {
						Game.game.anxietyLevel += riseAnxietyLevelByIfHits;
						playerMouth.PlayOneShot(breathingVeryFast);
					} else {
						Game.game.anxietyLevel += riseAnxietyLevelByIfGrowls;						
						playerMouth.PlayOneShot(breathingFast);
					}
				} else {
					Game.game.anxietyLevel += riseAnxietyLevelIfMonsterNOTGrowlsAttacks;
					monsterMouth.PlayOneShot(poof);
					if (particlesWhenPoof != null) {
						foreach (ParticleEmitter ps in particlesWhenPoof) {
							ps.enabled = true;
						}
					}		
					if (particleGlowWhenPoof != null) {
						particleGlowWhenPoof.enabled = true;
					}
				}
			} 			
			
			float currentTime = Time.time;
				
			Debug.Log("Time waited: " + (currentTime-startTime) + ", Start time: " + startTime + ", Current Time: " + currentTime);
			
			if (currentTime-startTime >= waitSecondsBeforeVanish) {
				calculating = false;
				eventDone = true;
			}
			
			if (!calculating && eventDone){
				if (!growls) {			
					playerHearth.PlayOneShot(heartbeat);
					playerMouth.PlayOneShot(breathingModerate);
				}
				Debug.Log("Event done!");
				//characterMotor.canControl = true;
				GameObject.Destroy(monsterMoveScript.monster);
				GameObject.Destroy(this.gameObject);
			}
		}
	}
	
	void OnTriggerEnter(Collider collision) {
		if (collision.tag == playerTag && !triggered) {
			doTrigger();
        }
	}
	
	void doTrigger() {
		Debug.Log("Event triggered!");
		
		if (monsterMoveScript != null) {
			float newSpeed = monsterMoveScript.startSpeed * 2.2f;				
			monsterMoveScript.speed = newSpeed;
			
			if (changeAnimationSpeed) {
				monsterMoveScript.monster.animation[walkAnimation].speed = newSpeed;
			}
			if (monsterMoveScript.monster.audio != null) {
				monsterMoveScript.monster.audio.enabled = true;
			}
		}
		monsterMoveScript.triggered = true;
		monsterMoveScript.monster.animation.Play(walkAnimation);
		triggered = true;
		//characterMotor.canControl = false;
	}
}
